package cn.com.manager.controller.purchase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.com.manager.controller.BaseController;
import cn.com.manager.page.PageInfo;
import cn.com.manager.pojo.PurchaseOrder;
import cn.com.manager.pojo.PurchaseOrderDetail;
import cn.com.manager.pojo.vo.PurchaseOrderVo;
import cn.com.manager.pojo.vo.PurchaseOrderWithProcess;
import cn.com.manager.service.PurchaseService;

@Controller
@RequestMapping("/purchase")
public class PurchaseController extends BaseController{
	@Autowired
	private PurchaseService purchaseService;
	
	/**
	 * 创建采购单页面
	 * @param purchaseOrder
	 * @param model
	 * @return
	 */
	@RequestMapping("/createPurchaseOrder")
	public String createPurchaseOrder(@ModelAttribute("purchaseOrderVo")PurchaseOrderVo purchaseOrderVo,@RequestParam(value="operate",required=false)String operate,Model model){
		if(purchaseOrderVo!=null && purchaseOrderVo.getPurchaseOrderWithProcess()!=null && StringUtils.isNoneBlank(purchaseOrderVo.getPurchaseOrderWithProcess().getId())){
			PurchaseOrderWithProcess purchaseOrderWithProcess = purchaseService.getPurchaseOrderInfo(purchaseOrderVo.getPurchaseOrderWithProcess().getId());
			purchaseOrderVo = new PurchaseOrderVo();
			purchaseOrderVo.setPurchaseOrderWithProcess(purchaseOrderWithProcess);
			model.addAttribute("purchaseOrderVo",purchaseOrderVo);
			model.addAttribute("operate",StringUtils.isNoneBlank(operate)?"look":"");
		}
		return "purchase/createPurchaseOrder";
	}
	
	/**
	 * 保存采购单信息
	 * @param purchaseOrder
	 * @param purchaseDetailInfos
	 * @param model
	 * @return
	 */
	@RequestMapping("/savePurchaseOrder")
	public String savePurchaseOrder(@ModelAttribute("purchaseOrderVo")PurchaseOrderVo purchaseOrderVo,@RequestParam("purchaseDetailInfos")String purchaseDetailInfos,Model model){
		//设置明细参数
		setPurchaseOrderParam(purchaseOrderVo.getPurchaseOrderWithProcess(),purchaseDetailInfos);
		//保存采购单信息
		boolean isNew = true;
		if(StringUtils.isNoneBlank(purchaseOrderVo.getPurchaseOrderWithProcess().getId())){
			isNew = false;
		}
		purchaseService.addPurchaseOrder(purchaseOrderVo.getPurchaseOrderWithProcess());
		if(isNew){
			//启动流程
			purchaseService.createPurchaseOrderInstance(purchaseOrderVo.getPurchaseOrderWithProcess().getId(),getCurrentUser().getId());
		}
		//获取采购单信息，并跳转到编辑页面
		PurchaseOrderWithProcess purchaseOrderInfo = purchaseService.getPurchaseOrderInfo(purchaseOrderVo.getPurchaseOrderWithProcess().getId());
		purchaseOrderVo = new PurchaseOrderVo();
		purchaseOrderVo.setPurchaseOrderWithProcess(purchaseOrderInfo);
		model.addAttribute("purchaseOrderVo",purchaseOrderVo);
		return "purchase/createPurchaseOrder";
	}
	

	/**
	 * 当前用户创建的采购单列表
	 * @param purchaseOrder
	 * @param model
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping("/purchaseOrderList")
	public String purchaseOrderTaskList(@ModelAttribute("purchaseOrder")PurchaseOrder purchaseOrder,Model model, PageInfo pageInfo){
		List<PurchaseOrder> purchaseOrderList = purchaseService.getPurchaseOrderList(purchaseOrder,getCurrentUser().getAccount(),pageInfo);
		model.addAttribute("purchaseOrderList",purchaseOrderList);
		model.addAttribute("purchaseOrder",purchaseOrder);
		return "purchase/purchaseOrderList";
	}
	
	
	/**
	 * 当前用户需要处理的任务列表
	 * @param purchaseOrderWithProcess
	 * @param model
	 * @return
	 */
	@RequestMapping("/purchaseOrderTaskList")
	public String purchaseOrderTaskList(@ModelAttribute("purchaseOrderWithProcess")PurchaseOrderWithProcess purchaseOrderWithProcess,Model model, PageInfo pageInfo){
		List<PurchaseOrderWithProcess> purchaseOrderTaskList = purchaseService.getPurchaseOrderTaskList(purchaseOrderWithProcess,getCurrentUser().getId(),pageInfo);
		model.addAttribute("purchaseOrderTaskList",purchaseOrderTaskList);
		model.addAttribute("purchaseOrderWithProcess",purchaseOrderWithProcess);
		return "purchase/purchaseOrderTaskList";
	}
	
	/**
	 * 提交采购单
	 * @param taskId
	 * @return
	 */
	@RequestMapping("submitPurchaseOrder")
	public String submitPurchaseOrder(@RequestParam(value="taskId",required=false)String taskId,@RequestParam(value="orderId",required=true)String orderId){
		//提交采购单
		purchaseService.submitPurchaseOrder(taskId, getCurrentUser().getId());
		//修改订单状态为正在处理中
		PurchaseOrder order = new PurchaseOrder();
		order.setId(orderId);
		order.setStatus("1");
		purchaseService.updatePurchaseOrderStatus(order);
		return "redirect:purchaseOrderTaskList";
	}
	
	
	/**
	 * 开始审核
	 * @param purchaseOrderWithProcess
	 * @param model
	 * @return
	 */
	@RequestMapping("/auditPurchaseOrder")
	public String auditPurchaseOrder(@ModelAttribute("PurchaseOrderWithProcess")PurchaseOrderWithProcess purchaseOrderWithProcess,Model model){
		PurchaseOrderWithProcess purchaseOrderInfo = purchaseService.getPurchaseOrderInfo(purchaseOrderWithProcess.getId());
		purchaseOrderInfo.setTaskDefinitionKey(purchaseOrderWithProcess.getTaskDefinitionKey());
		purchaseOrderInfo.setTaskId(purchaseOrderWithProcess.getTaskId());
		purchaseOrderInfo.setTaskName(purchaseOrderWithProcess.getTaskName());
		
		PurchaseOrderVo purchaseOrderVo = new PurchaseOrderVo();
		purchaseOrderVo.setPurchaseOrderWithProcess(purchaseOrderInfo);
		model.addAttribute("purchaseOrderVo",purchaseOrderVo);
		return "purchase/createPurchaseOrder";
	}
	
	/**
	 * 提交审核结果
	 * @param purchaseOrderVo
	 * @return
	 */
	@RequestMapping("/submitPurchaseOrderAudit")
	public String submitPurchaseOrderAudit(@ModelAttribute("purchaseOrderVo")PurchaseOrderVo purchaseOrderVo){
		purchaseOrderVo.getPurchaseOrderAudit().setUserId(getCurrentUser().getId());
		purchaseService.auditPurchaseOrder(purchaseOrderVo);
		return "redirect:purchaseOrderTaskList";
	}
	
	/**
	 * 查询当前正在运行的流程
	 * @param purchaseOrderWithProcess
	 * @param model
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping("/purchaseOrderInstanceList")
	public String purchaseOrderInstanceList(@ModelAttribute("purchaseOrderWithProcess") PurchaseOrderWithProcess purchaseOrderWithProcess, Model model, PageInfo pageInfo) {
		List<PurchaseOrderWithProcess> instanceList = purchaseService.findPurchaseOrderInstance(purchaseOrderWithProcess,pageInfo,getCurrentUser().getAccount());
		model.addAttribute("instanceList",instanceList);
		model.addAttribute("purchaseOrderWithProcess",purchaseOrderWithProcess);
		return "purchase/purchaseOrderInstanceList";
	}
	
	/**
	 * 查询历史采购单
	 * @param purchaseOrderWithProcess
	 * @param model
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping("/purchaseOrderHistoryInstanceList")
	public String purchaseOrderHistoryInstanceList(@ModelAttribute("purchaseOrderWithProcess") PurchaseOrderWithProcess purchaseOrderWithProcess, Model model, PageInfo pageInfo){
		List<PurchaseOrderWithProcess> instanceList = purchaseService.findHistoryPurchaseOrderInstance(purchaseOrderWithProcess, pageInfo,getCurrentUser().getAccount());
		model.addAttribute("instanceList",instanceList);
		model.addAttribute("purchaseOrderWithProcess",purchaseOrderWithProcess);
		return "purchase/purchaseOrderHistoryInstanceList";
	}
	
	/**
	 * 采购单统计
	 * @param purchaseOrder
	 * @param model
	 * @return
	 */
	@RequestMapping("/purchaseOrderCount")
	public String purchaseOrderCount(@ModelAttribute("purchaseOrder") PurchaseOrder purchaseOrder, Model model){
		Map<String, Double> purchaseOrderCount = purchaseService.getPurchaseOrderCount(purchaseOrder);
		model.addAttribute("purchaseOrderCount",purchaseOrderCount);
		return "purchase/purchaseOrderCount";
	}
	
	
	/**
	 * 历史任务(可以查看某一次采购经历的节点)
	 * @param instanceId
	 * @param model
	 * @return
	 */
	@RequestMapping("/purchaseOrderHistoryTask")
	public String purchaseOrderHistoryTask(@RequestParam(value="instanceId",required=true)String instanceId,Model model){
		List<PurchaseOrderWithProcess> taskList = purchaseService.findHistoryPurchaseOrderTask(instanceId);
		model.addAttribute("taskList",taskList);
		return "purchase/historyTaskList";
	}
	
	public void setPurchaseOrderParam(PurchaseOrderWithProcess purchaseOrderWithProcess,String purchaseDetailInfos){
		double totalPrice = 0;
		if(StringUtils.isNotBlank(purchaseDetailInfos)){
			List<PurchaseOrderDetail> purchaseOrderDetails = new ArrayList<PurchaseOrderDetail>();
			List<String> details  = Arrays.asList(purchaseDetailInfos.split(","));
			for(String detail : details){
				PurchaseOrderDetail bean = new PurchaseOrderDetail();
				Object [] message = detail.split("!");
				bean.setName(String.valueOf(message[0]));
				bean.setSize(String.valueOf(message[1]));
				bean.setUnit(String.valueOf(message[2]));
				bean.setQuantity(Integer.valueOf(String.valueOf(message[3])));
				bean.setPrice(Double.valueOf(String.valueOf(message[4])));
				bean.setDescription(String.valueOf(message[5]));
				purchaseOrderDetails.add(bean);
				totalPrice +=bean.getPrice()*bean.getQuantity(); 
			}
			purchaseOrderWithProcess.setPurchaseOrderDetails(purchaseOrderDetails);
		}
		
		purchaseOrderWithProcess.setCreateTime(new Date());
		purchaseOrderWithProcess.setCreateUser(getCurrentUser().getAccount());
		purchaseOrderWithProcess.setBudget(totalPrice);
	}
	
	/**
	 * 当前用户可拾取的任务列表
	 * @param purchaseOrderWithProcess
	 * @param model
	 * @return
	 */
	@RequestMapping("/canPurchaseOrderTaskList")
	public String canPurchaseOrderTaskList(@ModelAttribute("purchaseOrderWithProcess")PurchaseOrderWithProcess purchaseOrderWithProcess,Model model, PageInfo pageInfo){
		List<PurchaseOrderWithProcess> purchaseOrderTaskList = purchaseService.getCanPurchaseOrderTaskList(purchaseOrderWithProcess,getCurrentUser().getId(),pageInfo);
		model.addAttribute("purchaseOrderTaskList",purchaseOrderTaskList);
		model.addAttribute("purchaseOrderWithProcess",purchaseOrderWithProcess);
		return "purchase/canPurchaseOrderTaskList";
	}
	
	/**
	 * 用户拾取任务
	 * @param taskId
	 * @return
	 */
	@RequestMapping("/pickUpTask")
	public String pickUpTask(@RequestParam(value="taskId",required=true)String taskId){
		purchaseService.pickUpTask(getCurrentUser().getId(), taskId);
		return "redirect:purchaseOrderTaskList";
	}
	
	/**
	 * 结算
	 * @param taskId
	 * @param model
	 * @return
	 */
	@RequestMapping("/settleAccounts")
	public String settleAccounts(@RequestParam("taskId")String taskId,Model model){
		purchaseService.complateTask(getCurrentUser().getId(), taskId);
		return "redirect:purchaseOrderTaskList";
	}
	
	/**
	 * 入库
	 * @param taskId
	 * @param model
	 * @return
	 */
	@RequestMapping("/store")
	public String store(@RequestParam("taskId")String taskId,Model model){
		purchaseService.complateTask(getCurrentUser().getId(), taskId);
		return "redirect:purchaseOrderTaskList";
	}
}
